﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Text.RegularExpressions;
using System.Globalization;

namespace QLXMDataAccessLayer.Logic
{
    public class Functions
    {
        // ===== Validation Start ====================================================
        #region Validation
        public static bool isNullOrEmpty(object obj)
        {
            try
            {
                if (obj == null)
                    return true;
                if (obj.ToString().Trim().Equals(""))
                    return true;
                return false;
            }
            catch
            {
                return false;
            }
        }

        public static bool IsNumeric(object Expression)
        {
            // Variable to collect the Return value of the TryParse method.
            bool isNum;

            // Define variable to collect out parameter of the TryParse method. If the conversion fails, the out parameter is zero.
            double retNum;

            // The TryParse method converts a string in a specified style and culture-specific format to its double-precision floating point number equivalent.
            // The TryParse method does not generate an exception if the conversion fails. If the conversion passes, True is returned. If it does not, False is returned.
            isNum = Double.TryParse(Convert.ToString(Expression), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum);
            return isNum;
        }

        public static bool isValidMaxLength(object obj, long maxLength)
        {
            try
            {
                if (isNullOrEmpty(obj))
                    return true;
                if (obj.ToString().Trim().Length <= maxLength)
                    return true;

                return false;
            }
            catch
            {
                return false;
            }
        }

        public static bool IsValidEmailAddress(string sEmail)
        {
            if (sEmail == null)
            {
                return false;
            }

            int nFirstAT = sEmail.IndexOf('@');
            int nLastAT = sEmail.LastIndexOf('@');

            if ((nFirstAT > 0) && (nLastAT == nFirstAT) &&
            (nFirstAT < (sEmail.Length - 1)))
            {
                // address is ok regarding the single @ sign
                return (Regex.IsMatch(sEmail, @"(\w+)@(\w+)\.(\w+)"));
            }
            else
            {
                return false;
            }
        }

        //only accepted format: yyyy/MM or yyyy-MM
        public static bool isDate(string strDate)
        {
            string valuePassed = string.Empty;
            valuePassed = strDate;
            DateTime dt = DateTime.MinValue;

            try
            {
                string strRegex = @"^\d{4}([/])\d{1,2}([/])\d{1,2}$";
                Regex re = new Regex(strRegex);
                if (!re.IsMatch(strDate))
                    return false;

                IFormatProvider format = new CultureInfo("en-US");

                dt = DateTime.Parse(valuePassed, format, DateTimeStyles.None);
                return true;
            }
            catch
            {
                return false;
            }
        }
        #endregion
        // ===== Validation End ======================================================

        // ===== Converting Start ====================================================
        #region Converting
        public static long parseLong(object obj)
        {
            try
            {
                if (isNullOrEmpty(obj))
                    return 0;
                return long.Parse(obj.ToString());
            }
            catch
            {
                return 0;
            }
        }

        public static int parseInt(object obj)
        {
            try
            {
                if (isNullOrEmpty(obj))
                    return 0;
                return int.Parse(obj.ToString());
            }
            catch
            {
                return 0;
            }
        }

        public static double parseDouble(object obj)
        {
            try
            {
                if (isNullOrEmpty(obj))
                    return 0;
                return double.Parse(obj.ToString());
            }
            catch
            {
                return 0;
            }
        }

        public static decimal parseDecimal(object obj)
        {
            try
            {
                if (isNullOrEmpty(obj))
                    return 0;
                return decimal.Parse(obj.ToString());
            }
            catch
            {
                return 0;
            }
        }

        public static string parseString(object obj)
        {
            try
            {
                if (isNullOrEmpty(obj))
                    return "";
                return obj.ToString();
            }
            catch
            {
                return "";
            }
        }

        public static DataTable parseDataTable(DataTable structDt, DataRow[] arrRow)
        {
            DataTable list = new DataTable();
            try
            {
                list = structDt.Clone();

                foreach (DataRow drtemp in arrRow)
                {
                    list.ImportRow(drtemp);
                }

            }
            catch
            {

            }
            return list;
        }

        public static string ConvertNumberToWord(string str)
        {
            string strNum1 = "";
            string strNum2 = "";
            string total = "";
            string strTemp = "";
            string strTemp2 = "";
            string result = "";
            str = str.Replace(",", "");

            if (str.Contains("."))
            {
                strNum1 = ConvertToWord(Convert.ToInt64(str.Substring(str.IndexOf(".") + 1, str.Length - str.IndexOf(".") - 1)));
                if (str.Contains(","))
                {
                    strTemp = str.Substring(0, str.IndexOf(","));
                    strTemp2 = str.Substring(strTemp.ToString().Length + 1, str.Length - (strTemp.ToString().Length + 4));
                    total = strTemp.ToString() + strTemp2.ToString();
                }
                else
                {
                    strTemp2 = str.Substring(0, str.IndexOf("."));
                    total = strTemp2.ToString();
                }
                strNum2 = ConvertToWord(Convert.ToInt64(total));

                if (strNum1 != "")
                {
                    return result = strNum2 + " USD AND " + strNum1 + " PENCES";
                }
                else
                {
                    return result = strNum2 + " USD";
                }
            }
            else
            {
                return result = ConvertToWord(Convert.ToInt64(str)) + " USD ";
            }
        }

        private static string ConvertToWord(long nNumber)
        {
            long CurrentNumber = nNumber;
            string sReturn = "";

            if (CurrentNumber >= 1000000000)
            {
                sReturn = sReturn + " " + GetWord(CurrentNumber / 1000000000, "BILLION");
                CurrentNumber = CurrentNumber % 1000000000;
            }
            if (CurrentNumber >= 1000000)
            {
                sReturn = sReturn + " " + GetWord(CurrentNumber / 1000000, "MILLION");
                CurrentNumber = CurrentNumber % 1000000;
            }
            if (CurrentNumber >= 1000)
            {
                sReturn = sReturn + " " + GetWord(CurrentNumber / 1000, "THOUSAND");
                CurrentNumber = CurrentNumber % 1000;
            }
            if (CurrentNumber >= 100)
            {
                sReturn = sReturn + " " + GetWord(CurrentNumber / 100, "HUNDRED");
                CurrentNumber = CurrentNumber % 100;
            }
            if (CurrentNumber >= 20)
            {
                sReturn = sReturn + " " + GetWord(CurrentNumber, "");
                CurrentNumber = CurrentNumber % 10;
            }
            else if (CurrentNumber > 0)
            {
                sReturn = sReturn + " " + GetWord(CurrentNumber, "");
                CurrentNumber = 0;
            }
            return sReturn.Replace("  ", " ").Trim();
        }

        private static string GetWord(long nNumber, string sPrefix)
        {
            long nCurrentNumber = nNumber;
            string sReturn = "";
            while (nCurrentNumber > 0)
            {
                if (nCurrentNumber > 100)
                {
                    sReturn = sReturn + " " + GetWord(nCurrentNumber / 100, "HUNDRED");
                    nCurrentNumber = nCurrentNumber % 100;
                }
                else if (nCurrentNumber > 20)
                {
                    sReturn = sReturn + " " + GetTwentyWord(nCurrentNumber / 10);
                    nCurrentNumber = nCurrentNumber % 10;
                }
                else
                {
                    sReturn = sReturn + " " + GetLessThanTwentyWord(nCurrentNumber);
                    nCurrentNumber = 0;
                }
            }
            sReturn = sReturn + " " + sPrefix;
            return sReturn;
        }

        private static string GetTwentyWord(long nNumber)
        {
            string sReturn = "";
            switch (nNumber)
            {
                case 2:
                    sReturn = "TWENTY";
                    break;
                case 3:
                    sReturn = "THIRTY";
                    break;
                case 4:
                    sReturn = "FORTY";
                    break;
                case 5:
                    sReturn = "FIFTY";
                    break;
                case 6:
                    sReturn = "SIXTY";
                    break;
                case 7:
                    sReturn = "SEVENTY";
                    break;
                case 8:
                    sReturn = "EIGHTY";
                    break;
                case 9:
                    sReturn = "NINETY";
                    break;
            }
            return sReturn;
        }

        private static string GetLessThanTwentyWord(long nNumber)
        {
            string sReturn = "";
            switch (nNumber)
            {
                case 1:
                    sReturn = "ONE";
                    break;
                case 2:
                    sReturn = "TWO";
                    break;
                case 3:
                    sReturn = "THREE";
                    break;
                case 4:
                    sReturn = "FOUR";
                    break;
                case 5:
                    sReturn = "FIVE";
                    break;
                case 6:
                    sReturn = "SIX";
                    break;
                case 7:
                    sReturn = "SEVEN";
                    break;
                case 8:
                    sReturn = "EIGHT";
                    break;
                case 9:
                    sReturn = "NINE";
                    break;
                case 10:
                    sReturn = "TEN";
                    break;
                case 11:
                    sReturn = "ELEVEN";
                    break;
                case 12:
                    sReturn = "TWELVE";
                    break;
                case 13:
                    sReturn = "THIRTEEN";
                    break;
                case 14:
                    sReturn = "FORTEEN";
                    break;
                case 15:
                    sReturn = "FIFTEEN";
                    break;
                case 16:
                    sReturn = "SIXTEEN";
                    break;
                case 17:
                    sReturn = "SEVENTEEN";
                    break;
                case 18:
                    sReturn = "EIGHTEEN";
                    break;
                case 19:
                    sReturn = "NINETEEN";
                    break;
            }
            return sReturn;
        }

        public static string getMinDate(string minDate, bool fromToFomat)
        {
            string date = "";
            minDate = DateTime.Parse(minDate).ToShortDateString();
            int year = 0;
            year = DateTime.Parse(minDate).Year;
            int month = 0;
            month = DateTime.Parse(minDate).Month;
            int day = DateTime.Parse(minDate).Day;
            string slash = "/";
            if (fromToFomat == false)
            {
                slash = "";
            }
            if (month < 10)
            {
                if (day < 10)
                {
                    date = year.ToString() + slash + "0" + month.ToString() + slash + "0" + day.ToString();
                }
                else
                {
                    date = year.ToString() + slash + "0" + month.ToString() + slash + day.ToString();
                }
            }
            else
            {
                date = year.ToString() + slash + month.ToString() + slash + day.ToString();
            }
            return date;
        }

        public static string getMenuDate(string minDate, bool fromToFomat)
        {
            string date = "";
            minDate = DateTime.Parse(minDate).ToShortDateString();
            int year = 0;
            year = DateTime.Parse(minDate).Year;
            int month = 0;
            month = DateTime.Parse(minDate).Month;
            int day = 1;            
            string slash = "/";
            if (fromToFomat == false)
            {
                day = DateTime.Parse(minDate).Day;
            }
            if (month < 10)
            {
                if (day < 10)
                {
                    date = year.ToString() + slash + "0" + month.ToString() + slash + "0" + day.ToString();
                }
                else
                {
                    date = year.ToString() + slash + "0" + month.ToString() + slash + day.ToString();
                }
            }
            else
            {
                date = year.ToString() + slash + month.ToString() + slash + day.ToString();
            }
            return date;
        }
        public static string getMenuMaxDate(string minDate, bool fromToFomat)
        {
            string date = "";
            DateTime dt = DateTime.Parse(minDate);
            minDate = DateTime.Parse(minDate).ToShortDateString();
            int year = 0;
            year = DateTime.Parse(minDate).Year;
            int month = 0;
            month = DateTime.Parse(minDate).Month;
            string max = year +"/" + month + "/" + "01";
            int day = 0;               
            string slash = "/";
            if (fromToFomat)
            {
                dt = DateTime.Parse(max).AddMonths(1);
                dt = DateTime.Parse(dt.ToShortDateString()).AddDays(-1);
            }
            year = DateTime.Parse(dt.ToShortDateString()).Year;
            month = DateTime.Parse(dt.ToShortDateString()).Month;
            day = DateTime.Parse(dt.ToShortDateString()).Day;
            if (month < 10)
            {
                if (day < 10)
                {
                    date = year.ToString() + slash + "0" + month.ToString() + slash + "0" + day.ToString();
                }
                else
                {
                    date = year.ToString() + slash + "0" + month.ToString() + slash + day.ToString();
                }
            }
            else
            {
                date = year.ToString() + slash + month.ToString() + slash + day.ToString();
            }    
            return date;
        }      
        public static string getMaxDate(string maxDate, bool fromToFomat)
        {
            string date = "";
            int year = 0;
            int month = 0;
            int day = 0;
           

            string slash = "/";
            if (fromToFomat == false)
            {
                slash = "";
            }          
            year = DateTime.Parse(maxDate).Year;
            month = DateTime.Parse(maxDate).Month;
            day = DateTime.Parse(maxDate).Day;
            if (month < 10)
            {
                if (day < 10)
                {
                    date = year.ToString() + slash + "0" + month.ToString() + slash + "0" + day.ToString();
                }
                else
                {
                    date = year.ToString() + slash + "0" + month.ToString() + slash + day.ToString();
                }
            }
            else
            {
                date = year.ToString() + slash + month.ToString() + slash + day.ToString();
            }
            return date;
        }

        public static string getDateVNEN(string dateConvert)
        {
            string date = "";
            int year = 0;
            int day = 0;
            int month = 0;
            year = DateTime.Parse(dateConvert).Year;
            month = DateTime.Parse(dateConvert).Month;
            day = DateTime.Parse(dateConvert).Day;
            if (month < 10)
            {
                if (day < 10)
                {
                    date = "0" + day.ToString() + "/0" + month.ToString() + "/" + year.ToString();
                }
                else
                {
                    date = day.ToString() + "/0" + month.ToString() + "/" + year.ToString();
                }
            }
            else
            {
                date = day.ToString() + "/" + month.ToString() + "/" + year.ToString();
            }
            return date;
        }
        public static string getCommonDate(string minDate)
        {
            string date = "";
            minDate = DateTime.Parse(minDate).ToShortDateString();
            int year = 0;
            year = DateTime.Parse(minDate).Year;
            int month = 0;
            month = DateTime.Parse(minDate).Month;
            int day = DateTime.Parse(minDate).Day;
            string slash = "/";           
            if (month < 10)
            {
                if (day < 10)
                {
                    date = year.ToString() + slash + "0" + month.ToString() + slash + "0" + day.ToString();
                }
                else
                {
                    date = year.ToString() + slash + "0" + month.ToString() + slash + day.ToString();
                }
            }
            else
            {
                date = year.ToString() + slash + month.ToString() + slash + day.ToString();
            }
            return date;
        }
        #endregion     
        
    }
}